//
//  RJCStationLineView.m
//  AirQuality
//
//  Created by Po on 15/8/31.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCStationLineView.h"
#import "RJCStationLine.h"
@interface RJCStationLineView ()
@property (retain, nonatomic) RJCStationLine * lineView;                //折线图数据
@property (retain, nonatomic) NSDictionary * data;              //数据
@property (assign, nonatomic) CGFloat maxHeight;                //最大值
@property (assign, nonatomic) CGFloat maxHeightd;               //偏移最大值
@property (assign, nonatomic) NSInteger yLabelWidth;            //纵轴宽度
@property (assign, nonatomic) BOOL isCO;

- (void)initInterface;
@end

@implementation RJCStationLineView

- (instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)data
{
    self = [super initWithFrame:frame];
    if (self) {
        if (data) {
            _data = data;
            _isCO = [[[_data[@"data"][0] allKeys] lastObject] isEqualToString:@"co"];
        }
//        NSLog(@"%@",data);
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    [self addSubview:[self getLineView]];
}

- (void)setStatusString:(NSString *)string
{
    [_lineView.showStatusLabel setText:string];
}

- (RJCStationLine *)getLineView
{
    if (!_lineView) {
        CGRect rect = self.bounds;
        _yLabelWidth = DeviceType >= 6 ? 40 : 30;
        rect.origin.x += _yLabelWidth;
        rect.size.width -= (_yLabelWidth * 2 + 10);
        [self updateDataWithDeviationValue];
        
        if (_data.count != 0) {
            [self initYaxis];
        }
        _lineView = [[RJCStationLine alloc] initWithFrame:rect data:_data maxHeight:_maxHeight];
    }
    return _lineView;
}

/**更新偏移数据*/
- (void)updateDataWithDeviationValue
{
    _maxHeight = [self getMaxHeightValue];
    if (!_data) {
        return;
    }
    CGFloat maxHeightRange = _maxHeight * 2 / 3;
    NSDictionary * deviationValue = _data[@"data"][2];
    NSArray * deviations = [[deviationValue allValues] lastObject];
    CGFloat deviationMaxValue = [self getDeviationMaxValue:deviations];
    _maxHeightd = deviationMaxValue * 3 / 2;
    CGFloat deviationLength = deviationMaxValue * 2;        //偏差范围
    CGFloat unitLenght = maxHeightRange / deviationLength;  //折线图偏差相对于数据的单位大小
    CGFloat zeroPoint = _maxHeight / 2;                     //设置偏差0点位置
    
    NSMutableArray * tempDeviations = [NSMutableArray array];
    for (NSString * value in deviations) {
        CGFloat valueFloat = [value floatValue];
        CGFloat temp = zeroPoint + (unitLenght * valueFloat);
        [tempDeviations addObject:GetStringWithFloat(temp)];
    }
    NSString * tempKey = [_data[@"data"][0] allKeys][0];
    NSArray * lineData = _data[@"data"][0];
    NSArray * cityData = _data[@"data"][1];
    NSArray * timeData = _data[@"times"];
    _data = @{@"data":@[
                      lineData,
                      cityData,
                      @{tempKey:tempDeviations
                        }
                      ],
              
              @"times":timeData
              };
}

- (CGFloat)getDeviationMaxValue:(NSArray *)array
{
    CGFloat maxNum = 0;
    for (NSString * num in array) {
        CGFloat value = fabs([num floatValue]);
        if (value > maxNum) {
            maxNum = value;
        }
    }
    return maxNum;
}

/**初始化Y轴*/
- (void)initYaxis
{
    CGFloat height = (self.bounds.size.height - 60) / 6;
    CGFloat labelHeight = height / 4 * 3;
    //    CGFloat labelTopMargin = height / 4;
    CGRect rect = CGRectMake(0,0, _yLabelWidth, labelHeight);
    CGRect rRect = CGRectMake(ScreenWidth - _yLabelWidth - 15, 0, _yLabelWidth + 10, labelHeight);
    
    CGFloat rUnitLenght = _maxHeightd / 3;
    CGFloat tempDvalue = _maxHeightd;
//    CGFloat tempDvalue = 15;
    for (NSUInteger i = 0; i < 7; i ++) {
        rect.origin.y = height * (i + 1) - height / 2 + 5;
        if (_isCO) {
            float unit =_maxHeight / 6;               //平均每一段的值
            [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:@"%.2f",(unit  * (6 - i))]]];
//            if (i == 6) {
//                [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:@"%.1f",-unit]]];
//            }else{
//             [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:@"%.1f",unit * (6 - i)]]];
//            }
            [self addSubview:[self getLabelWithFrame:CGRectMake(0,0, _yLabelWidth, height / 2 + 5) title:@"(mg)"]];
        } else {
            NSUInteger unit =  _maxHeight / 6;               //平均每一段的值
//            [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:[NSString stringWithFormat:@"%ld",unit * (5 - i)]]];
            [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:@"%ld",unit * (6 - i)]]];
            [self addSubview:[self getLabelWithFrame:CGRectMake(0,0, _yLabelWidth, height / 2 + 5) title:@"(ug)"]];
        }
        
        //右侧
        rRect.origin.y = height * (i + 1) - height / 2 + 5;
        
        NSString * num = [NSString stringWithFormat:@"%.1f",tempDvalue];
        UILabel * label = [self getLabelWithFrame:rRect title:num];
        [label setTextColor:GetRGBColor(68, 41, 119, 1)];
        [label setTextAlignment:NSTextAlignmentRight];
        [self addSubview:label];
        tempDvalue -= rUnitLenght;
//        tempDvalue -= 3;
    }
}

- (UILabel *)getLabelWithFrame:(CGRect)rect title:(NSString *)title
{
    CGFloat lableFond = (DeviceType >= 6) ? (rect.size.height - 2):(rect.size.height - 3);
    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    [label setText:title];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:GetRGBColor(85, 85, 85, 1)];
    [label setFont:[UIFont systemFontOfSize:lableFond]];
    return label;
}

- (CGFloat)getMaxHeightValue
{
    if (_data && _data.count != 0) {
        return [self getMaxNumberLine];
    }
    return 0;
}

- (CGFloat)getMaxNumberLine
{
    CGFloat finalMax = 0;
    NSArray * tempData = _data[@"data"];
    for (NSUInteger i = 0; i < tempData.count; i ++) {
        NSDictionary * dataDic = tempData[i];
        NSArray * allValue = [[dataDic allValues] lastObject];
        if (allValue.count == 0) {
            continue;
        }
        CGFloat maxNum = [allValue[0] floatValue];
        for (NSUInteger j = 0; j < [allValue count]; j ++) {
            CGFloat num = [allValue[j] floatValue];
            if (maxNum < num) {
                maxNum = num;
            }
        }
        if (finalMax < maxNum) {
            finalMax = maxNum;
        }
    }
    //计算最贴近哪条线
    if (_isCO) {
        return ((NSUInteger)finalMax + 1);
    } else {
        return [self getMaxLineNumWithNum:finalMax];
    }
}

/**非CO的最大值计算*/
- (CGFloat)getMaxLineNumWithNum:(CGFloat)num
{
    NSUInteger temp = (NSUInteger)(num) + 50;
    if ((temp % 50) > 25) {
        return ((temp / 50) + 1) * 50;
    } else {
        return (temp / 50) * 50;
    }
}

@end
